package com.fulihui.wgj.biz.impl;

import com.fulihui.common.util.PlatformUtil;
import com.fulihui.weixinsdk.WeixinSDK;
import com.fulihui.weixinsdk.model.UserInfo;
import com.fulihui.weixinsdk.request.material.MaterialMpNewsGetSingleReuest;
import com.fulihui.weixinsdk.request.user.UserInfoGetRequest;
import com.fulihui.weixinsdk.result.material.MaterialMpNewsGetSingleResult;
import com.fulihui.weixinsdk.result.user.UserInfoGetResult;
import com.fulihui.wgj.biz.WeiXinSdkBiz;
import com.fulihui.wgj.dal.impala.domain.WxUserInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 微信sdk接口业务处理
 *
 * @author: Levon
 * @date: 2018-07-26 14:38
 */
@Service
public class WeiXinSdkBizImpl implements WeiXinSdkBiz {

    private final static Logger LOGGER = LoggerFactory.getLogger(WeiXinSdkBizImpl.class);

    @Autowired
    private PlatformUtil platformUtil;

    @Autowired
    private WeixinSDK weixinSDK;

    @Override
    public UserInfoGetResult getUserDetail(String appId, String openId) {
        String accessToken = platformUtil.get(appId);
        if (StringUtils.isBlank(accessToken)) {
            LOGGER.info("GetUserDetail!!get token is null");
            return null;
        }
        UserInfoGetRequest userGetRequest = new UserInfoGetRequest();
        userGetRequest.setAccess_token(accessToken);
        userGetRequest.setOpenid(openId);
        UserInfoGetResult userInfoGetResult = weixinSDK.invokeService(userGetRequest);
        if (userInfoGetResult != null && userInfoGetResult.isSuccess()) {
            return userInfoGetResult;
        }
        return null;
    }

    @Override
    public WxUserInfo weiXinModel2DbModel(UserInfoGetResult result) {
        if (result == null || result.getUserInfo() == null) {
            return null;
        }
        UserInfo resultUser = result.getUserInfo();
        WxUserInfo userInfo = new WxUserInfo();
        userInfo.setCity(resultUser.getCity());
        userInfo.setCountry(resultUser.getCountry());
        userInfo.setProvince(resultUser.getProvince());
        userInfo.setGroupid(resultUser.getGroupid());

        userInfo.setLanguage(resultUser.getLanguage());
        userInfo.setNickname(resultUser.getNickname());
        userInfo.setHeadimgurl(resultUser.getHeadimgurl());

        userInfo.setSex(resultUser.getSex());
        userInfo.setOpenid(resultUser.getOpenid());
        userInfo.setUnionid(resultUser.getUnionid());

        userInfo.setSubscribe(resultUser.getSubscribe());
        userInfo.setSubscribeTime(resultUser.getSubscribe_time());
        userInfo.setRemark(resultUser.getRemark());
        return userInfo;
    }

    @Override
    public Boolean mpMaterialIsUseful(String token, String mediaId) {
        LOGGER.info("mpMaterialIsUseful token={}, mediaId={}", token, mediaId);
        if (StringUtils.isBlank(token) || StringUtils.isBlank(mediaId)) {
            return false;
        }
        MaterialMpNewsGetSingleReuest materialMpNewsGetSingleReuest = new MaterialMpNewsGetSingleReuest();
        materialMpNewsGetSingleReuest.setAccess_token(token);
        materialMpNewsGetSingleReuest.setMedia_id(mediaId);
        MaterialMpNewsGetSingleResult result;
        try {
            result = weixinSDK.invokeService(materialMpNewsGetSingleReuest);
        } catch (Exception e) {
            LOGGER.info("mpMaterialIsUseful error" + e);
            return false;
        }
        return result.isSuccess();
    }
}
